package com.sun.config;/**
 * @author sun
 * @date 2022/1/4 - 14:54
 */

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 登录成功的逻辑
 * @author 孙
 * @date 2022/1/4 14:54
 * @version 1.0
 */
public class MyForwardAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
  private String url;

    public MyForwardAuthenticationSuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        System.out.println(authentication.getAuthorities());
        //安全考虑，被设为null
        System.out.println(authentication.getCredentials());
        System.out.println(authentication.getDetails());
        WebAuthenticationDetails webAuthenticationDetails = (WebAuthenticationDetails) authentication.getDetails();

        System.out.println(webAuthenticationDetails.getRemoteAddress());
        //ip地址和sessionid在WebAuthenticationDetails中被获取
        System.out.println(authentication.getPrincipal());
        response.sendRedirect(url);
    }
}
